/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers.classes;

import classes.Breadcrumbs;
import classes.HibernateUtil;
import dao.SearchDao;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import models.Course;
import models.Employee;
import models.Page;
import models.PrivateMessage;
import models.Student;
import models.User;

/**
 * This controller search through the entire site with the given keywords and
 * tries to find matches to any keyword on the site.
 *
 * @author Sander
 */
public class SearchController extends HttpServlet {

    /**
     * This doGet splits the parameter k into pieces (into multiple keywords)
     * then it will search in: Courses, Students, Employees, Pages and even 
     * when a member is logged in Private messages.
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // Split the search keywords from the url
        String[] k = request.getParameter("k").toLowerCase().split("\\s+");

        // If there are no keywords send redirect
        if (k[0].equals("")) {
            response.sendRedirect("Index");
            return;
        }

        // Initialize private message block variable
        String pm = "false";

        // Creating a string with all the keywords and links
        StringBuilder sb = new StringBuilder();
        sb.append("<a href=\"Search?k=")
                .append(k[0])
                .append("\">")
                .append(k[0])
                .append("</a>");
        for (int i = 1; i < k.length; i++) {
            sb.append(", ")
                    .append("<a href=\"Search?k=")
                    .append(k[i])
                    .append("\">")
                    .append(k[i])
                    .append("</a>");
        }
        request.setAttribute("keywords", sb.toString());

        // Initialize SearchDao
        HibernateUtil hu = new HibernateUtil();
        SearchDao dao = new SearchDao(hu.openSession());

        // Getting all courses with use of the keywords
        List<Course> courses = dao.findCourses(k);
        request.setAttribute("results_courses", courses);

        // Getting all students who are not hidden with use of the keywords
        List<Student> students = dao.findStudents(k);
        request.setAttribute("results_students", students);

        // Getting all teachers with use of the keywords
        List<Employee> teachers = dao.findTeachers(k);
        request.setAttribute("results_teachers", teachers);

        // Getting all pages with use of the keywords
        List<Page> pages = dao.findPages(k);
        request.setAttribute("results_pages", pages);

        HttpSession session = request.getSession();
        if (session.getAttribute("userrole") != null) {
            User user = (User) request.getSession().getAttribute("user");

            // Getting all private messages with use of the keywords
            List<PrivateMessage> private_messages = dao.findPrivateMessages(k, user.getMember_id());
            request.setAttribute("results_private_messages", private_messages);

            // Change value of private message block variable
            pm = "true";
        }

        request.setAttribute("pm", pm);
        
        // Breadcrumbs
        Breadcrumbs[] breadcrumbs = {
            new Breadcrumbs("Index", "Home"),
            new Breadcrumbs("Search", "Search")
        };
        request.setAttribute("breadcrumbs", breadcrumbs);

        hu.close();
        
        RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/search.jsp");
        dispatcher.forward(request, response);
    }
}
